package com.dps.omc.utils;

import com.dps.omc.model.ErrorKeyWord;
import com.dps.omc.service.IErrorKeyWordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lip on 2016-04-01.
 */
@Component
public class ErrorAnalyzer implements ApplicationListener<ContextRefreshedEvent> {

    public boolean analyse(String msg)
    {
        for(String key:map.keySet())
        {
            if(msg.contains(key))
            {
                return true;
            }
        }
        return false;
    }

    private final static Logger logger = LoggerFactory.getLogger(SysSetting.class);

    @Autowired
    private IErrorKeyWordService errorKeyWordService;

    private static Map<String,String> map;

    public void reload(){
        loadMap();
    }

    private void loadMap(){
        logger.info("=========初始化日志错误关键字开始========");
        map = new HashMap<String, String>();
        List<ErrorKeyWord> words = errorKeyWordService.findAll();
        for(ErrorKeyWord word:words)
        {
            map.put(word.getKeyWord(),word.getKeyWord());
        }
        logger.info("=========初始化日志错误关键字完毕========");
    }

//    public static String getValue(String key)
//    {
//        return map.get(key);
//    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        loadMap();
    }

//
}
